package huachenxing.mimi520.main.widgets
{
	import com.cambright.stack.widgets.DockWidget;
	
	import flash.desktop.NativeApplication;
	import flash.display.Bitmap;
	import flash.display.NativeMenuItem;
	
	import huachenxing.mimi520.brower.commands.EndBrowerCommand;
	import huachenxing.mimi520.brower.commands.StartBrowerCommand;
	import huachenxing.mimi520.main.commands.AlertArgs;
	import huachenxing.mimi520.option.commands.OpenAccountWindowCommand;
	import huachenxing.mimi520.option.commands.OpenQueryWindowCommand;
	import huachenxing.mimi520.option.commands.OpenWorkerWindowCommand;
	import huachenxing.mimi520.resource.Icon;
	import huachenxing.mimi520.tools.Tips;

	public class DockWidget extends com.cambright.stack.widgets.DockWidget
	{
		private var _startItem:NativeMenuItem;
		private var _endItem:NativeMenuItem;
		private var _optionItem:NativeMenuItem;
		
		override protected function invoke():void
		{
			super.invoke();
			tooltip = Tips.APP_NAME;
			_setIcon(Icon.APP);
		}
		
		override protected function setMenu():void
		{
			with(menu)
			{
				addItem("控制台");
				{
					next();
					_startItem = addItem("开始", _start);
					_endItem = addItem("停止", _end, false);
					back();
				}
				_optionItem = addItem("设置");
				{
					next();
					addItem("账号", OpenAccountWindowCommand);
					addItem("作业", OpenWorkerWindowCommand);
					addItem("查询", OpenQueryWindowCommand);
					back();
				}
				addItem();
				addItem("退出", _onMenuExitSelected);
			}
		}
		
		private function _start():void
		{
			_startItem.enabled = false;
			_endItem.enabled = true;
			_optionItem.enabled = false;
			
			var cmd:StartBrowerCommand = new StartBrowerCommand();
			cmd.dispatch();
		}
		
		private function _end():void
		{
			_startItem.enabled = true;
			_endItem.enabled = false;
			_optionItem.enabled = true;
			
			var cmd:EndBrowerCommand = new EndBrowerCommand();
			cmd.dispatch();
		}
		
		public function setTips(alert:AlertArgs):void
		{
			tooltip = alert.msg;
		}
		
		private function _onMenuExitSelected():void
		{
			_app.exit();
		}
		
		private function _setIcon(definition:Class):void
		{
			_app.icon.bitmaps = [(new definition() as Bitmap).bitmapData];
		}
		
		private function get _app():NativeApplication
		{
			return NativeApplication.nativeApplication;
		}
	}
}